<?php
/**
 * Created by PhpStorm.
 * User: liaohonglai
 * Date: 2017/10/27
 * Time: 9:36
 */

namespace app\admin\controller;

use think\Db;
use think\Request;
class Erecord extends \think\Controller
{
    public function index(){
        return $this->fetch();
    }

    public function getList($pageNumber=0,$pageSize=5,$searchText='%'){
       $datas =  Db::table('oes_erecordnew')->where('id = :id or euser_id Like :euser_id or teacher_id Like :teacher_id',['id'=>$searchText,'euser_id'=>'%'.$searchText.'%','teacher_id'=>'%'.$searchText.'%'])->paginate($pageSize,false,['page'=>$pageNumber]);
        $users = Db::table("user")->select();
        $teacher_id = Db::table("oes_emp")->select();
        $clazz_id = Db::table("oes_clazz")->select();
        $course_id = Db::table("oes_course")->select();
        $etask_id = Db::table("oes_etask")->select();
        $etype_id = Db::table("oes_etype")->select();
        $newdata=array();
        foreach ($datas as &$itemss) {
            $item = $itemss;
            //替换euser_id为评价人
            foreach ($users as &$items){
                if($item['euser_id'] == $items['id']){
                    $item['euser_id'] = $items['username'];
                    break;
                }
            }

            //替换teacher_id为被评价人
            foreach ($teacher_id as $teacher){
                if($item['teacher_id'] == $teacher['id']){
                    $item['teacher_id'] = $teacher['name'];
                    break;
                }
            }
            //替换clazz_id 为班级名称
            foreach ($clazz_id as $clazz){
                if($item['clazz_id'] == $clazz['id']){
                    $item['clazz_id'] = $clazz['name'];
                    break;
                }
            }

            //替换course_id 为课程名称
            foreach ($course_id as $course){
                if($item['course_id'] == $course['id']){
                    $item['course_id'] = $course['name'];
                    break;
                }
            }

            //替换etask_id 为任务名称
            foreach ($etask_id as $etask){
                if($item['etask_id'] == $etask['id']){
                    $item['etask_id'] = $etask['name'];
                    break;
                }
            }

            //替换etype_id 为评价类型
            foreach ($etype_id as $etype){
                if($item['etype_id'] == $etype['id']){
                    $item['etype_id'] = $etype['name'];
                    break;
                }
            }

            array_push($newdata,$item);
        }
        return json([$datas, $newdata]);
    }
}